var express = require('express');
var router = express.Router();

const filmsManageService = require("../service/filmsManageService");

//新增电影
router.post('/', async function (req, res) {
    let { name, ename, type, area, time, upDate, year, score, count, want, intro, directorName, directorImg, actorName, actorFilmName, actorImg, images } = req.body;
    let director = [{
        name: directorName,
        image: directorImg
    }];
    let actor = [{
        name: actorName,
        filmName: actorFilmName,
        image: actorImg
    }];
    let data = await filmsManageService.insert({ name, ename, type, area, time, upDate, year, score, count, want, intro, director, actor, images });
    res.send(data);
})

//删除电影
router.delete('/:id', async function (req, res) {
    let { id } = req.params;
    let data = await filmsManageService.delete(id);
    res.send(data);
});

//修改电影
router.put('/:id', async function (req, res) {
    let { name, ename, type, area, time, upDate, year, score, count, want, intro, directorName, directorImg, actorName, actorFilmName, actorImg, images } = req.body;
    let director = [{
        name: directorName,
        image: directorImg
    }];
    let actor = [{
        name: actorName,
        filmName: actorFilmName,
        image: actorImg
    }];
    let { id } = req.params;
    let data = await filmsManageService.update({ name, ename, type, area, time, upDate, year, score, count, want, intro, director, actor, images, _id: id });
    res.send(data);
})
//

//查询所有电影
router.get('/', async function (req, res) {
    let { type, value, current, pageSize } = req.query;
    let searchParams = {};
    try {
        if (value) {
            searchParams[type] = new RegExp(value,'i');
        }
        let data = await filmsManageService.find({ current: current || 1, pageSize: pageSize || 5 }, searchParams);
        res.send(data);
    } catch (error) {
        console.log(error);
    }

})
//通过ID查询
router.get('/:id', async function (req, res) {
    let { id } = req.params;
    let data = await filmsManageService.findById(id);
    res.send(data);
})
module.exports = router;